package com.ftg.learn.chapter08;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.jwt.JWTUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kangjx
 * @createTime 2022/11/11
 */
public class LearnJwt {

    private static final String salt = "admin";

    /**
     *  <version>0.9.1</version> 这个版本是可以用的
     *
     *
     *  0.11.5 我们发现要引入 jjwt-api jjwt-impl own.jar
     *
     * @param args
     */
    public static void main(String[] args) {
//        String token = createToken();
//        System.out.println("生成的令牌为: " + token);

        String saltHutool = "sys_user_token";
        Map<String,Object> map = new HashMap<>(3);
        map.put("id",1);
        map.put("account","admin");
        map.put("telphone","12323432432");

        String hutoolToken = JWTUtil.createToken(map,saltHutool.getBytes(StandardCharsets.UTF_8));
        System.out.println("hutool 令牌" + hutoolToken);


        System.out.println(JWTUtil.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50IjoiYWRtaW4iLCJpZCI6MSwidGVscGhvbmUiOiIxMjMyMzQzMjQzMiJ9.-Mjzu6rOUlQomMGD2oMdweiH1mcWJX_5CymuEs6g-es", saltHutool.getBytes(StandardCharsets.UTF_8)));

        String name = "李宇祥";

        //8个字节
        String salt = "dianliu6";

        /**
         * InvalidKeyException: Wrong key size
         * des 密码的长充为 16bit  salt长度不够
         */
        DES key = SecureUtil.des(salt.getBytes());

        String mw1 = key.encryptBase64(name);

        System.out.println(mw1);

        String mw2 = key.encryptHex(name);
        System.out.println(mw2);
//
//
//        String context = SecureUtil.des().encryptHex(name);
//        System.out.println(context);



//        Claims userinfo = decodeToken("eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0c3RzcnNkZnNmZHMiLCJleHAiOjE2NjgxMzI4NDEsInVzZXJpZCI6MTIzLCJ1c2VyYWNjb3VudCI6ImFkbWluIiwianRpIjoiMTIzIn0.2f38K2Zmzo7v_eSYBatJKaWWlcRTN7WVzyRgTo7urwB3hsOhlz2pQiIt0LgCg8_lT8FMdVHa2_t3S4NTPlaeiw");
//
//        System.out.println("id为" + userinfo.getId());
//        System.out.println("subject为" + userinfo.getSubject());
//
//        System.out.println("自定义Claims 取里面的useraccount \t" + userinfo.get("useraccount").toString());
//        System.out.println("自定义Claims 取里面的 userid\t" + userinfo.get("userid").toString());
//        System.out.println("当前时间 \t" + System.currentTimeMillis());
//        System.out.println("有效时间 \t" + userinfo.getExpiration().getTime());

    }



    public static String createToken(){
        Map<String,Object> map = new HashMap<>(2);
        map.put("userid",123);
        map.put("useraccount","admin");

        long times = System.currentTimeMillis();
        long creament = 1000 * 100;

        String str = Jwts.builder()
                //自定义数据
                .setClaims(map)
                //原始数据
                // 编号
                .setId("123")
                // 内容
                .setSubject("tstsrsdfsfds")
                .signWith(SignatureAlgorithm.HS512, salt).compact();
        return str;

    }

    public static Claims decodeToken(String token){
        Claims body = Jwts.parser()
                .setSigningKey(salt)
                .parseClaimsJws(token)
                .getBody();
        return body;
    }

}
